package com.spzx.product.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.github.pagehelper.PageHelper;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.product.domain.Product;
import com.spzx.product.domain.ProductDetails;
import com.spzx.product.domain.ProductSku;
import com.spzx.product.query.SkuQuery;
import com.spzx.product.service.*;
import com.spzx.product.vo.*;
import io.swagger.v3.core.util.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/channel")
public class ChannelController extends BaseController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private ProductSkuService productSkuService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private SkuStockService skuStockService;
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductDetailsService productDetailsService;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/index")
    public AjaxResult getIndexData() {
        List<CategoryVo> categoryVoList = categoryService.getCategoryList();
        List<ProductSkuVo> productSkuVoList = productSkuService.getProductSkuList();
        IndexDataVo indexDataVo = new IndexDataVo();
        indexDataVo.setCategoryList(categoryVoList);
        indexDataVo.setProductSkuList(productSkuVoList);
        return AjaxResult.success(indexDataVo);
    }
    @GetMapping("/category")
    public AjaxResult getCategory(){
       List<CategoryVo> list= categoryService.tree();
       return AjaxResult.success(list);
    }
    @GetMapping("/brand")
    public AjaxResult getBrand(){
        return AjaxResult.success(brandService.list());
    }
    @GetMapping("/skuList/{pageNum}/{pageSize}")
    public TableDataInfo getPageSkuList(
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize,
            SkuQuery skuQuery
    ){
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        List<ProductSkuVo> list = productSkuService.getPageSkuList(skuQuery);
        return getDataTable(list);
    }
    @GetMapping("/item/{skuId}")
    public AjaxResult getItem(@PathVariable Long skuId){
        //先从redis中查询bitmap,避免缓存穿透
        //先设置从redis中缓存位图数据的key
        String key = "product:sku:data";
        Boolean bit = redisTemplate.opsForValue().getBit(key, skuId);
        if(!bit){
            throw new ServiceException("商品不存在");
        }
        ItemVo itemVo = new ItemVo();
        //获取sku信息
        ProductSku productSku = productSkuService.getProductSkuById(skuId);
        itemVo.setProductSku(productSku);
        //获取最新价格
        SkuPriceVo skuPriceVo = productSkuService.getSkuPrice(skuId);
        itemVo.setSkuPrice(skuPriceVo);
        //获取库存信息
        SkuStockVo skuStockVo = skuStockService.getSkuStock(skuId);
        itemVo.setSkuStock  (skuStockVo);
        //获取商品id
        Long productId = productSku.getProductId();
        //获取商品信息
        Product product = productService.getById(productId);
        String[] split = product.getSliderUrls().split(",");
        itemVo.setSliderUrlList(split);
        //获取商品规格信息
        String specValue = product.getSpecValue();
        JSONArray jsonArray = JSONArray.parseArray(specValue);
        itemVo.setSpecValueList(jsonArray);
        //获取商品详情信息
        String[] detailsImageUrlList = productDetailsService.getDetailsImageUrlList(productId);
        itemVo.setDetailsImageUrlList(detailsImageUrlList);
        //获取商品规格值
        Map<String,Long> skuSpecValueMap = productSkuService.getSkuSpecValueMap(productId);
        itemVo.setSkuSpecValueMap(skuSpecValueMap);
        return AjaxResult.success(itemVo);
    }
}
